// pages/login/login.js
//获取应用实例
const app = getApp()

Page({
  data: {
    username: '',
    password: '',
    showImg: false,
    imgWindow: true,
    isLogin: true,
    isReg: false,
    userChoiseImg: '',
    imgStr: '',
    showModalStatus: false,
    imgData: [],
    envId: 'cloud1-2gu4iyfmadd39493',
  },
  onLoad(options) {
    this.getImgList();
  },
  //获取系统配置的头像列表数据
  getImgList() {
    wx.cloud.callFunction({
      name: 'homePlayFuntions',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'getUserImages'
      }
    }).then((resp) => {
      this.setData({
        imgData: resp.result.data
      });
    }).catch((e) => {
      console.log(e);
    });
  },

  handleRadio(e) {
    var that = this;
    var choisedImg = e.currentTarget.dataset.img
    var choiseId = e.currentTarget.dataset.imgid
    var itemsArray = that.data.imgData
    itemsArray.forEach(function (item, index) {
      item.checked = (choiseId == item.id ? true : false)
    })
    that.setData({
      imgData: itemsArray,
      userChoiseImg: choisedImg
    })
  },
  showGameToast() {
    this.setData({
      showModalStatus: !this.data.showModalStatus
    })
  },
  //获取输入框内容
  nameChange(e) {
    this.data.username = e.detail.value;
  },
  pwdChange(e) {
    this.setData({
      password: e.detail.value
    })
  },
  //登录事件
  userLogin(userInfo) {
    var that = this;
    if (userInfo.userName == '' || userInfo.userPassword == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入账号和密码',
      })
    } else {
      wx.cloud.callFunction({
        name: 'homePlayFuntions',
        config: {
          env: that.data.envId
        },
        data: {
          type: 'userLogin',
          user: userInfo
        }
      }).then((resp) => {
        if (resp.result.data.length != 0) {
          if (resp.result.data[0].userPassword == userInfo.userPassword) {
            wx.navigateTo({
              url: '/pages/games/games' + '?username=' + resp.result.data[0].userName + '&leader=' + resp.result.data[0].leader + "&id=" + resp.result.data[0]._id + "&img=" + resp.result.data[0].img,
            })
          } else {
            wx.showToast({
              icon: "none",
              title: '用户名或密码错误'
            });
          }
        } else {
          wx.showModal({
            title: '提示',
            content: "该用户不存在，请先注册",
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                that.setData({
                  isLogin: false,
                  isReg: true
                })
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
              }
            }
          });
        }
      }).catch((e) => {
        console.log(e);
      });
    }
  },
  //注册事件
  userGester(userInfo) {
    var that = this;
    that.setData({
      showImg: true
    });
    var img = that.data.userChoiseImg;
    var username = userInfo.userName
    var password = userInfo.userPassword
    var checkpwd = userInfo.checkPassword
    var user = userInfo;
    user.leader = 0;
    user.img = img;
    user.createTime = app.getNowDate();
    if (username == '' || password == '' || img == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入账号和密码和头像',
      })
    } else if (password != checkpwd) {
      wx.showToast({
        icon: 'none',
        title: '两次密码输入不一致',
      })
    } else {
      wx.cloud.callFunction({
        name: 'homePlayFuntions',
        config: {
          env: that.data.envId
        },
        data: {
          type: 'userRegester',
          user: user
        }
      }).then((resp) => {
        that.setData({
          username: "",
          password: ""
        });
        wx.navigateTo({
          url: '/pages/index/index'
        })
      }).catch((e) => {
        console.log(e);
      });
    }
  },
  LoginOrGester(e) {
    var that = this;
    var userInfo = e.detail.value;
    var subType = e.detail.target.dataset.sub
    if (subType == 1) {
      that.userGester(userInfo)
    } else {
      that.userLogin(userInfo);
    }
  },

})